package com.cskaoyan.service;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.basebean.BasePageData;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.bean.footprint.FootprintVo;
import com.cskaoyan.bean.user.Footprint;
import com.cskaoyan.bean.user.FootprintExample;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.utils.MarketPageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author 姚旭佳
 */
@Service
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public BasePageData footprintList(BaseParam param) {
        MarketPageUtil.startPage(param);
        FootprintExample example = new FootprintExample();
        FootprintExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(getUserId());
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause("update_time desc");
        List<Footprint> footprints = footprintMapper.selectByExample(example);
        List<FootprintVo> footprintVos=new ArrayList<>();
        for (Footprint footprint : footprints) {
            Goods goods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            FootprintVo vo = new FootprintVo();
            vo.setBrief(goods.getBrief());
            vo.setPicUrl(goods.getPicUrl());
            vo.setAddTime(footprint.getUpdateTime());
            vo.setName(goods.getName());
            vo.setRetailPrice(goods.getRetailPrice());
            vo.setId(footprint.getId());
            vo.setGoodsId(footprint.getGoodsId());
            footprintVos.add(vo);
        }
        return BasePageData.ok(footprintVos);
    }

    @Override
    public void addFootprint(Integer id) {
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals()==null){
            return;
        }
        FootprintExample example = new FootprintExample();
        FootprintExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(getUserId());
        criteria.andDeletedEqualTo(false);
        criteria.andGoodsIdEqualTo(id);
        List<Footprint> footprints = footprintMapper.selectByExample(example);
        if(footprints!=null&&footprints.size()>0){
            footprints.get(0).setUpdateTime(new Date());
            footprintMapper.updateByPrimaryKeySelective(footprints.get(0));
            return;
        }
        Footprint footprint = new Footprint();
        footprint.setUserId(getUserId());
        footprint.setGoodsId(id);
        footprint.setAddTime(new Date());
        footprint.setUpdateTime(new Date());
        footprintMapper.insertSelective(footprint);
    }

    private Integer getUserId(){
        Subject subject = SecurityUtils.getSubject();
        Object primaryPrincipal = subject.getPrincipals().getPrimaryPrincipal();
        return ((User) primaryPrincipal).getId();

    }
}
